package com.report.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.constants.TagConstants;
import com.report.dto.request.TagReq;
import com.report.dto.response.TagRes;
import com.report.entity.Tag;
import com.report.mapper.TagMapper;
import com.report.service.ITagService;
import com.zmh.framework.redis.RedisCache;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:55:17
 */
@Service
@AllArgsConstructor
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Override
    public Page<TagRes> lists(TagReq tagReq) {
        Page<Tag> page = new Page<>(tagReq.getPageNum(), tagReq.getPageSize());
        LambdaQueryWrapper<Tag> eq = Wrappers.lambdaQuery(Tag.class).like(StrUtil.isNotEmpty(tagReq.getName()),Tag::getName, tagReq.getName()).eq(Tag::getMark, tagReq.getMark())
                .orderByDesc(Tag::getCreateTime);
        Page<Tag> page1 = this.page(page, eq);
        System.out.println(JSON.toJSONString(page1));
        return Convert.convert(new TypeReference<Page<TagRes>>() {},this.page(page,eq));
    }

    private final RedisCache redisCache;

    @Override
    public List<TagRes> tagLists(Integer markId) {

        List<Tag> tags = this.list(Wrappers.lambdaQuery(Tag.class).eq(Tag::getMark, markId));

        return Convert.toList(TagRes.class, tags);
    }

    @Override
    public void saveTag(Tag convert) {
        this.saveOrUpdate(convert);
        List<Tag> tags = this.list(Wrappers.lambdaQuery(Tag.class).eq(Tag::getMark, convert.getMark()));
        redisCache.set(TagConstants.TAG_MARK_LIST + convert.getMark(),JSON.toJSONString(tags),2, TimeUnit.HOURS);
    }

    @Override
    public List<TagRes> searchList(Integer pageNum,Integer pageSize,Integer markId) {
        LambdaQueryWrapper<Tag> eq = Wrappers.lambdaQuery(Tag.class).eq(Tag::getMark, markId);
        Page<Tag> page = this.page(new Page<>(pageNum, pageSize), eq);
        return Convert.toList(TagRes.class,page.getRecords());
    }



    @Override
    public Page<TagRes> hotSpotList(Integer pageNum, Integer pageSize, int markId) {
        Page<Tag> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Tag> eq = Wrappers.lambdaQuery(Tag.class).eq(Tag::getMark, markId);
        return Convert.convert(new TypeReference<Page<TagRes>>() {
        },this.page(page,eq));
    }

    private final BeanSearcher beanSearcher;

    @Override
    public List<TagRes> allTagLists() {

        return this.baseMapper.weChatMiniTagLists();
    }
}
